﻿@using CoralNode.Core.Entity.Domains.Node
@using CoralNode.Core.Entity.SearchQuerys.Node
@using CoralNode.MvcClient.Helper
@using Newtonsoft.Json
@{
    Layout = null;
    NodeSearchQuery query = ViewBag.Query;
}
<div class="pj_overlay" id="pj_overlay">
    <div class="olay_h">添加内容</div>
    <div class="olay_b clearfix  olay_t">
        <div class="cnt h_48  pb_1 clearfix adduser_box">
            <div class="funsearch  pt_0-5 pb_0-5 extra_normal" data-js="filemanage" data-delhref="#"
                data-savehref="#">
                <div class="f_r">
                    <form class="j_form" method="GET" action="/m/node/staticNode?value=@ViewBag.Value">
                        <label class="search f_l mr_1 clearfix">
                            <input class="text w_18" name="Title" value=""><input type="submit" value="" class="submit">
                            <input type="hidden" name="PrototypeId" value="@query.PrototypeId"/>
                            <input type="hidden" name="CategoryId" value="@query.CategoryId" />
                        </label>
                    </form>
                </div>
            </div>
            <div class="olay_cnt clearfix js_user_select">
                <div class="left clearfix h_46 nav_left">
                    <ul class="" style="width: 236px; left: 36px; display: block; position: relative;
                        top: 0; left: 0; border: 0; background: none;"
                        id="ztreePrototype"></ul>
                    <textarea class="js_ztree_data" style="display: none;">@ViewBag.StaticNodeCategoryData</textarea>
                </div>
                <div class="right">
                    <div class="membersort f_no">
                        <div class="sortbox clearfix js-ajax-content">
                            @foreach (NodeDomain item in Model.Items) {

                                <div class="memberbox" itemid="@item.Id">
                                    <div class="box">
                                        <div class="t">
                                            @UtilHelper.ToShortTitle(item.Title, 26)
                                        </div>
                                        <div class="cnt">
                                            <span>分类：@item.Category.Title</span>
                                        </div>
                                    </div>
                                    <label class="checkbox_own">
                                        <input type="checkbox" value='@JsonConvert.SerializeObject(new { item.Id, item.Title, item.Status })'></label>
                                </div>
                            }
                             @Html.IncludePage("CoralAdmin/Shared","Pager2",new{list=Model})
                        </div>
                    </div>
                </div>
            </div>
            <div class="usernum">
                已选择<span class="btn btn_green_small_style3  f_no ml_0-5 mr_0-5 js_manage_selectnum">0</span>个
                <input type="hidden" name="selectnum" value="" />
            </div>
        </div>
        <div class="olay_btn clearfix">
            <div class="r">
                <a class="btn btn_green_normal w_6 js-member-save" href="">保存</a>
                <a class="btn btn_gray_normal w_6 ml_1 js-close" href="javascript:void(0);">取消</a>
            </div>
        </div>
    </div>
</div>
<script>
    var $outer = $("#pj_overlay").parents(".d-outer");
    var params = $outer.data("params");
    $('.j_form').submit(function (e) {
        e.preventDefault();
        var element = $(this).attr("action") + " .js-ajax-content";
        $('.js-ajax-content').parent().load(element, $(this).serialize(), function () {
            $(".js_user_select").each(function () {
                var $this = $(this);
                changeManageEvent($this, 1);
            });
            $('.js_manage_selectnum').text(0);
        });
    });
    $('.js-member-save').click(function (e) {
        e.preventDefault();
        var nodes = new Array();
        $('.js-ajax-content .checkbox_own').each(function () {
            var $this = $(this);
            if ($this.hasClass('selected')) {
                nodes.push(JSON.parse($this.find('input').val()));
            }
        });
        if (nodes.length > 0) {
            if (params != null && params.okfun != null && typeof (params.okfun) === 'function') {
                eval("params.okfun(nodes)");
            }
        } else {
            $.alert("请选择成员");
        }
    });
    $('.js-close').live("click", function () {
        $('a.d-close').trigger("click");
    });
    $(".select_owner").dropdown_owner();
    //自定义checkbox
    $(".js_user_select").each(function () {
        var $this = $(this);
        changeManageEvent($this, 1);

    });
    $('.pager a').live("click", function (e) {
        e.preventDefault();
        var element = $(this).attr("href") + " .js-ajax-content";
        $('.js-ajax-content').parent().load(element, function () {
            $(".js_user_select").each(function () {
                var $this = $(this);
                var currentText = 0;
                $('.js_manage_selectnum').text(currentText);
                changeManageEvent($this, 1);
            });
        });

    });
    $('.js-letter').click(function (e) {
        e.preventDefault();
        $(this).parent().addClass('current').siblings().removeClass('current');
        var element = $(this).attr("href") + " .js-ajax-content";
        $('.js-ajax-content').parent().load(element, function () {
            $(".js_user_select").each(function () {
                var $this = $(this);
                changeManageEvent($this, 1);
            });
            $('.js_manage_selectnum').text(0);
        });
    });
    function changeManageEvent($currentbox, type) {
        type = type || 1;
        var $items = $currentbox.find(".memberbox:not('.adduser')");
        if (type == 1) {
            $currentbox.next().show();
            var $num = $currentbox.next().find(".js_manage_selectnum");
            $items.find(".checkbox_own").show().click(function () {
                $(this).trigger("click.own");
            });
            $items.css("cursor", "pointer").bind("click.change", function (e) {
                e.preventDefault();
                var $this = $(this);
                $this.find(".checkbox_own").trigger("click.own");
                $this.hasClass("memberboxcurrent") ? $this.removeClass("memberboxcurrent") : $this.addClass("memberboxcurrent");
                $num.text($items.filter(".memberboxcurrent").length);
            })
            $currentbox.find(".adduser").hide();
        }
        else {

            $items.filter(".memberboxcurrent").trigger("click");
            $items.find(".checkbox_own").hide();
            $items.unbind("click.change");
            $currentbox.find(".adduser").show();
            $currentbox.next().hide();
            $currentbox.next().find(".js_manage_selectnum").text("0");
        }
    }

    $(function () {
        function addDiyDom(treeId, treeNode) {
            var spanObj = $("#" + treeNode.tId + '_span');
            if (treeNode.pId == null) {
                spanObj.parent().attr("target", null);
                spanObj.addClass("tree_ico");
                spanObj.empty();
                spanObj.after(treeNode.name);
            } else {
                spanObj.parent().attr('href', '/m/node/staticnode?value=@ViewBag.Value&categoryId=' + treeNode.id);
                spanObj.parent().attr("target", null);
                spanObj.parent().parent().parent().css("display", "none");
                spanObj.after(treeNode.name);
                spanObj.remove();
            }

            if (treeNode.checked == true) {
                var liObj = $("#" + treeNode.tId);
                var $liObj = liObj;
                $liObj.addClass("current");
            }
        }

        var $ztree = $("#ztreePrototype");
        jQuery.fn.zTree.init($ztree, {
            view: {
                showLine: false,
                showIcon: false,
                selectedMulti: false,
                dblClickExpand: false,
                addDiyDom: addDiyDom
            },
            data: { simpleData: { enable: true } },
            callback: {
            }
        }, jQuery.parseJSON($ztree.next().val()));
        $('span.button').remove();

        var leftPid0 = $('div.nav_left > ul > li');

        $(leftPid0).each(function () {
            $($(this).children("ul").children('li')).each(function (i, n) {
                isChild($(n));
                initialize($(n));
            });
            if ($(this).hasClass('current')) $(this).children('ul').show();
        });



        function isChild(node) {//是否有子分类
            if (node.children('ul').length > 0) {
                node.children('ul').children("li").each(function () {
                    isChild($(this));
                });
                node.children('a').prepend('<span class="ico_tree"></span>');
                upDown(node.children('a'));
            } else {
                node.children('a').prepend('<span class="ico_tree_no"></span>');
            }
        }

        function upDown(node) {//展开收缩
            var child = node.next('ul');
            node.children('span.ico_tree').click(function (e) {
                e.preventDefault();
                e.stopPropagation();
                if (child.is(':hidden')) {
                    $(this).addClass('ico_tree_open');
                    child.slideDown();
                } else {
                    $(this).removeClass('ico_tree_open');
                    child.slideUp();
                }
            });
        }

        function initialize(node) {
            var items = node.children("ul").children("li");
            if (node.hasClass('current')) {
                $(node).parents('ul').show();
                $(node).parents('ul').prev('a').children('span.ico_tree').addClass('ico_tree_open');
                initializeRoot(node);
            }
            else {
                items.each(function () {
                    initialize($(this));
                });
            }
        }

        function initializeRoot(node) {
            if (node.parent("ul").parent("li").length != 0) {
                initializeRoot(node.parent("ul").parent("li"));
            }
            else {
                node.children("a").find("span").addClass("tree_ico_no").removeClass("tree_ico");
            }
        }

        leftPid0.children('a').click(function () {
            $(this).find("span").parents("li").siblings("li").children("a").removeClass("current").find("span").addClass("tree_ico").removeClass("tree_ico_no");
            if ($(this).find("span").hasClass("tree_ico")) {
                $(this).find("span").addClass("tree_ico_no").removeClass("tree_ico");
            }
            else {
                $(this).find("span").addClass("tree_ico").removeClass("tree_ico_no");
            }
            if ($(this).parent().find("li").length == 0) {
                $(this).parent("li").addClass("current");
                $(this).find("span").removeClass("tree_ico").removeClass("tree_ico_no").addClass("tree_ico");
            }
            var accordtion = $(this).next('ul'), thisSiblings = $(this).parent().siblings().children('ul');
            accordtion.is(':hidden') ? accordtion.slideDown() : accordtion.slideUp();
            if (thisSiblings.is(':visible')) thisSiblings.slideUp();
        });

        leftPid0.find("a").click(function (e) {
            e.preventDefault();
            $('.js-ajax-content').parent().load($(this).attr("href") + " .js-ajax-content", function () {
                $(".js_user_select").each(function () {
                    var $this = $(this);
                    changeManageEvent($this, 1);
                });
                $('.js_manage_selectnum').text(0);
            });
        });


    });
</script>
